package com.madev.isellcrazyitems;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.madev.isellcrazyitems.entity.Product;
import com.madev.isellcrazyitems.entity.Statement;
import com.madev.isellcrazyitems.handler.DatabaseHandler;
import com.madev.sales.R;

public class AddUpdateValues extends Activity implements OnClickListener {
	private Button btn_updaterecord;
	private EditText txtpname, txtpprice, txtstock;
	DatabaseHandler db;
	Product pm;
	Intent i;
	DatabaseHandler sc;
	Statement st;
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_updaterecord:
			if (txtpname.getText().toString().equals("")
					|| txtpprice.getText().toString().equals("")) {
				Toast.makeText(AddUpdateValues.this, "Please add values..",
						Toast.LENGTH_LONG).show();
			} else {

				db = new DatabaseHandler(getApplicationContext());
				db.getWritableDatabase();
				pm = new Product();
				pm.productName = txtpname.getText().toString();
				pm.productPrice = txtpprice.getText().toString();
				pm.productStock = txtstock.getText().toString();
				
				pm.productID = i.getExtras().getString("id");

				Log.i(">>>>>productid<<<<<", "" + i.getExtras().getString("id"));
				db.updateProduct(pm);
				

				sc = new DatabaseHandler(getApplicationContext());
				sc.getWritableDatabase();
				st = new Statement();
				st.statement = ("UPDATE Products SET productName='" + pm.getProductName() + "',productPrice='" + pm.getProductPrice() + "', " +
						"productStock='" + pm.getProductStock() + "' WHERE productIdNo='" + pm.getProductID() + "';");
				sc.addStatement(st);
				
				Toast.makeText(AddUpdateValues.this,
						"Record Update successfully.", Toast.LENGTH_LONG)
						.show();

				db.close();
				finish();
				super.onResume();
				
			}
			break;
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.updateproduct);

		i = getIntent();

		txtpname = (EditText) findViewById(R.id.txt_udatepname);
		txtpprice = (EditText) findViewById(R.id.txt_udatepprice);
		txtstock = (EditText) findViewById(R.id.txt_updatestock);
		
		txtstock.setText(i.getExtras().getString("stock"));
		txtpname.setText(i.getExtras().getString("name"));
		txtpprice.setText(i.getExtras().getString("price"));
		btn_updaterecord = (Button) findViewById(R.id.btn_updaterecord);
		btn_updaterecord.setOnClickListener(this);
	}

}
